package Scuttlebutt;

import java.io.*;
import java.lang.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.*;
import java.util.*;

public class Project extends Object implements Serializable 
{
	String projectTitle = null;
	File projectPath = null;
	JTree tree = null;
	String ftpAddress = null;
	int port = 21;
	String username = null;
	String password = null;
	ArrayList<String> isOpenList = null;
	
	public Project()
	{
		tree = new JTree(new FolderNode("Project", null));
		
		isOpenList = new ArrayList<String>();
	}
	
	public Project(String title){
		projectTitle = title;
		tree = new JTree(new FolderNode("Project", null));
		isOpenList = new ArrayList<String>();
	}
	
	public void setList(ArrayList list){
		isOpenList.addAll(list);
	}
	
	public ArrayList getList(){
		return isOpenList;
	}
	
	public void updateFTP(String address, int port, String username, String password){
		ftpAddress = address;
		this.port = port;
		this.username = username;
		this.password = password;
	}
	
	public String getFTPaddress(){
		return ftpAddress;
	}
	
	public int getPort(){
		return port;
	}
	
	public String getUsername(){
		return username;
	}
	
	public String getPassword(){
		return password;
	}
	
	public String getAddress(){
		return ftpAddress;
	}
	
	public void setProjectName(String name){
		projectTitle = name;
	}
	
	public void setProjectPath(File path){
		projectPath = path;
	}	
	
	public File getProjectPath(){
		return projectPath;
	}
	
	public String getProjectTitle(){
		return projectTitle;
	}
	
	public void removeOpened(String key){
		isOpenList.remove(key);
	}
	
	public void addOpened(String key){
		
		if(!isOpenList.contains(key)){
			String ext = getExtension(new File(key));
			if((ext.compareToIgnoreCase("html") == 0) ||
			(ext.compareToIgnoreCase("htm") == 0) ||
			(ext.compareToIgnoreCase("jsp") == 0) ||
			(ext.compareToIgnoreCase("py") == 0) ||
			(ext.compareToIgnoreCase("txt") == 0)){
				isOpenList.add(key);
			}
		}
	}
	
    public String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }
	
	
	public boolean isOpened(String key){
		return isOpenList.contains(key);
	}
	
	public int getOpendCount(){
		return isOpenList.size();
	}
	
	public Object[] getOpenedList(){
		return isOpenList.toArray();
	}

	public TreeModel getTreeModel(){
		return tree.getModel();
	}
	
	public void setTreeModel(TreeModel t){
		tree.setModel(t);
	}
	
	public void setTree(JTree tree){
		this.tree = tree;
	}
	
	
	public JTree getTree(){
		if(tree == null){
			System.out.println("Tree is null");
		}
		return tree;
	}
	
	public void clear(){
		projectTitle = new String();
		projectPath = null;//new File();
		FolderNode node = (FolderNode)tree.getModel().getRoot();
		node.removeAllChildren();
		node.setUserObject("project");
		node.setUploaded(true);
		node.setRemoteDirectory("");
		ftpAddress = new String();
		port = 21;
		username = new String();
		password = new String();
		isOpenList.clear();
	}
	
    static public void Save(Project p){
	    File f = new File(new String(p.getProjectPath() + "\\" + p.getProjectTitle() + "\\" + p.getProjectTitle() + ".prj"));
	    if(p != null){
	    	try{
		    	if(p.getProjectPath() != null){
		    		ObjectOutputStream s = new ObjectOutputStream(new FileOutputStream(f));
		    		s.writeObject(p);
		    	}
			}catch(IOException io){
				System.out.println("My exception thrown(Project): " + io.getMessage());
			}
	    }
    }   
    
    static public Project Open(File file){
    	try{
	    	ObjectInputStream s = new ObjectInputStream(new FileInputStream(file));
	    	Project p = (Project)s.readObject();
	    	
	    	return p;
		}catch(IOException io){
			System.err.println("My exception thrown: " + io.getMessage());
		}catch(ClassNotFoundException cnf){
			System.err.println(cnf.getMessage());
		}
		
		return new Project();
	}
	
	public Object SearchTree(Object root, String path){
		Object o = null;
		
		for (Enumeration e = ((DefaultMutableTreeNode)root).children() ; e.hasMoreElements() ;) {
			Object child = (Object)e.nextElement();
			System.out.println("child path:" + ((SortableNode)child).getNodePath() + " path:" + path);
			File f = new File(((SortableNode)child).getNodePath());
			if (child instanceof URLNode ){
				
			}else if(f.getPath().compareToIgnoreCase(path) == 0){
				o = child;
			}else{
				o = SearchTree(child, path);
			}
			
//			System.out.println("o:" + o.toString());
			
			if(o != null)
				break;
			
		}
		return o;
	}
	
}


